<template>
  <div class="app-container">
    
    
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="交易方向" v-if="$store.state.user['apikey']==='976c4e8fdde94643bf0927440976f6c8'">
        <el-radio-group v-model="tradePair" size="small" @change="queryParams.tradePair = tradePair">
          <el-radio-button label="USDT兑SUSD" ></el-radio-button>
          <el-radio-button label="SUDT兑USDT" ></el-radio-button>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="手机号" prop="phone"  v-else>
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          style="width: 240px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="tradeid">
        <el-input
          v-model="queryParams.tradeid"
          placeholder="订单编号"
          clearable
          style="width: 240px;"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单日期">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

  <div v-if="$store.state.user['apikey']==='976c4e8fdde94643bf0927440976f6c8'">
    底仓：
    <el-tag type="success" style="font-size:16px">{{usdtAmount}} USDT</el-tag>
    <el-tag type="info" style="font-size:16px">{{susdAmount}} SUSD</el-tag>
    <el-tag type="warning" style="font-size:16px">收益: {{profits}} USDT</el-tag>
    <br><br>
  </div>
<el-tag type="success" style="font-size:16px">成功入金数量：{{totalSummary.sourceamount.toFixed(2)}}</el-tag>
<el-tag type="info" style="font-size:16px">成功兑换数量：{{totalSummary.targetamount.toFixed(6)}}</el-tag>
<el-tag type="warning" style="font-size:16px">成功订单数：{{totalSummary.success}}</el-tag>
<el-tag type="danger" style="font-size:16px">失败订单数：{{totalSummary.failed}}</el-tag>
<el-tag style="font-size:16px">成功率：{{totalSummary.rate}}</el-tag>
<br><br>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="订单编号" align="center" prop="tradeid" />
      <el-table-column label="入金币种" align="center" prop="sourcecurrency" width="80"/>
      <el-table-column label="入金数量" align="center" prop="sourceamount" />
      <el-table-column label="兑换币种" align="center" prop="targetcurrency"  width="80"/>
      <el-table-column label="兑换数量" align="center" prop="targetamount" />
      <el-table-column label="汇率" align="center" prop="rate" width="100"/>
      <el-table-column label="时间" align="center" prop="createtime"/>
      <el-table-column label="订单状态" align="center">
        <template slot-scope="scope">
          <span style="color:green" v-if="scope.row.status==='complete'">{{scope.row.status}}</span>
          <span style="color:red" v-else>{{scope.row.status}}</span>
        </template>
      </el-table-column>
      <el-table-column label="分账地址/手机号" align="center" prop="toaddress"/>
      <el-table-column label="分账结果" align="center" prop="issueresult" v-if="$store.state.user['apikey']!=='976c4e8fdde94643bf0927440976f6c8'"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[100, 200, 300]"
      :page-size="100"
    />
  </div>
</template>

<script>
import { listOrder, searchOrderByTradeId, getProfits } from "@/api/bizmanage/order";
import { formatDate } from "@/utils/index";

export default {
  name: "BizOrders",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 状态数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        tradeid: undefined,
        phone: undefined,
        status: undefined,
        tradePair: undefined
      },
      // 统计
      totalSummary:{
        sourceamount:0,
        targetamount:0,
        success:0,
        failed:0,
        rate:0
      },
      usdtAmount:0,
      susdAmount:0,
      profits:0,
      tradePair: 'USDT兑SUSD'
    };
  },
  created() {
    var today = new Date();
    // today.setTime(today.getTime() + 24*60*60*1000);
    var todayStr = today.getFullYear()+"-" + (today.getMonth()+1) + "-" + today.getDate();

    // var day7before = new Date();
    // // day7before.setTime(day7before.getTime() - 24*60*60*1000);
    // var day7beforeStr = day7before.getFullYear()+"-"+(day7before.getMonth()+1)+"-"+day7before.getDate();
    this.dateRange = [todayStr,todayStr]
  
    this.getList();
  
    getProfits(this.$store.state.user['apikey']).then(response => {
      /*"data": {
        "sourcecurrency": "usdt",
        "targetcurrency": "susd",
        "sourceamount": 1662.094540000000000000000000000000000000,
        "targetamount": 79148.854930000000000000,
        "profits": 227.475460000000000000000000000000000000
    } */
            if(response.data != null){            
              this.usdtAmount = response.data.sourceamount;
              this.susdAmount = response.data.targetamount;
              
              this.profits = response.data.profits;
            }

        });
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
    

      if (this.queryParams.tradeid !== undefined && this.queryParams.tradeid.trim().length > 0){
          searchOrderByTradeId(this.$store.state.user['apikey'], this.queryParams.tradeid.trim()).then(response => {
            this.list = []
            if(response.data != null){
              this.list.push(response.data);
              this.total = 1;
              for(let i=0; i<this.list.length; i++){
                this.list[i].createtime = formatDate(new Date(this.list[i].createtime));
                this.list[i].sourcecurrency = this.list[i].sourcecurrency.toUpperCase();
                this.list[i].targetcurrency = this.list[i].targetcurrency.toUpperCase();
              }
            }

            this.loading = false;
        });
      }else{
        
        listOrder(this.$store.state.user['apikey'], this.dateRange[0], this.dateRange[1], this.queryParams.pageNum, this.queryParams.pageSize, this.queryParams.phone,this.queryParams.tradePair).then(response => {
            if(response.data.list === undefined || response.data.total === 0 || response.data.list === null || response.data.stats === null){
              // 没有数据
              response.data.list = []
              this.totalSummary={
                sourceamount:0,
                targetamount:0,
                success:0,
                failed:0,
                rate:0
              }
              this.loading = false;
              return 
            }
            this.list = response.data.list;
            this.total = response.data.total;
            this.totalSummary={
              sourceamount:response.data.stats.sourceamount,
              targetamount:response.data.stats.targetamount,
              success:response.data.stats.successOrderNumber,
              failed:response.data.stats.failOrderNumber,
              rate:(response.data.stats.successRate*100) + '%'
            }

            for(let i=0; i<this.list.length;i++){
              this.list[i].createtime = formatDate(new Date(this.list[i].createtime))
              this.list[i].sourcecurrency = this.list[i].sourcecurrency.toUpperCase();
              this.list[i].targetcurrency = this.list[i].targetcurrency.toUpperCase();
            }

            // let orderList = response.data.list;
            // let startIndex = (this.queryParams.pageNum - 1) * this.queryParams.pageSize ;
            // let endIndex = this.queryParams.pageNum * this.queryParams.pageSize ;
            // this.list = []
            // this.totalSummary={
            //   sourceamount:0,
            //   targetamount:0,
            //   success:0,
            //   failed:0,
            //   rate:0
            // }
            // this.total = response.data.total;
            // for(let i=0; i<orderList.length; i++){
            //   if(orderList[i].status==='complete'){
            //     this.totalSummary.sourceamount += orderList[i].sourceamount;
            //     this.totalSummary.targetamount += orderList[i].targetamount;
            //     this.totalSummary.success += 1;
            //   }else if(orderList[i].status!=='created'){                
            //     this.totalSummary.failed += 1;
            //   }

            //   // 只显示选择的页
            //   if(i>=startIndex && i<endIndex){
            //     orderList[i].createtime = formatDate(new Date(orderList[i].createtime));
            //     orderList[i].sourcecurrency = orderList[i].sourcecurrency.toUpperCase();
            //     orderList[i].targetcurrency = orderList[i].targetcurrency.toUpperCase();
            //     this.list.push(orderList[i])
            //   }
            // }
            // this.totalSummary.rate = (this.totalSummary.success*100/orderList.length).toFixed(2) + '%'
            this.loading = false;

          }
        );
        
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
     
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>

